
package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductCategoryMapper;
import org.itboys.mobile.dao.product.SundayProductMapper;

import org.itboys.mobile.dto.SundayPermissionDto;
import org.itboys.mobile.entity.mongo.SundayPermission;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_关联_权限。
 *
 */
/*
@Service
public class SundayPermissionService extends BaseMongoService<SundayPermission> {
	@Autowired
	private SundayProductCategoryMapper categoryMapper;
	@Autowired
	private SundayProductMapper productMapper;


	
	private static final long serialVersionUID = -5806434055381127192L;

	@Resource(name="mobileDs")
	private MongoDataSource sundayDataSource;
	@Override
	protected MongoDataSource getMongoDataSource() {
		return sundayDataSource;
	}
	@Override
	protected Class<SundayPermission> getEntityClass() {
		return SundayPermission.class;
	}


	*//**
     * 按用户获取分类-小分类-商品树，（含库存和价格）
	 * @param memberId
     * @return
     *//*
	public List<SundayPermissionDto> getPermissionTree(Long memberId){
		List<SundayPermissionDto> tree_cat_parents = new ArrayList<SundayPermissionDto>();
		List<SundayPermissionDto> tree_cat_children = new ArrayList<SundayPermissionDto>();
		List<SundayPermissionDto> tree_products = new ArrayList<SundayPermissionDto>();

		Map<String,Object> param = new HashMap<String,Object>();
			//1,获取所有分类

		List<SundayProductCategory> allCategorys=categoryMapper.select(param);
		param.clear();
		//1.1，获取分类树形数据
	*//*	List<SundayPermissionDto> parent_cats=new ArrayList<SundayPermissionDto>();
		List<SundayPermissionDto> child_cats=new ArrayList<SundayPermissionDto>();*//*
		for(SundayProductCategory allCategory:allCategorys){
				SundayPermissionDto permissionDto =
						new SundayPermissionDto(
								allCategory.getId(), allCategory.getName(),
								allCategory.getParentId(), null,2,
								null, null,
								null);
				if(allCategory.getParentId()==0l){
					tree_cat_parents.add(permissionDto);
				}else{
					tree_cat_children.add(permissionDto);
				}
		}
			//2，获取所有商品
		param.clear();
		param.put("type", SundayCommonConstants.type_mall);
		param.put("status",1);
		List<SundayProduct> allProducts =productMapper.select(param);

			//3，获取所有权限
		param.clear();
		param.put("memberId",memberId);
		param.put("type",1);
		param.put("isDeleted",0);

		List<SundayPermission> product_pers = super.list(param);
		//4，组装树形结构
		//4.1,匹配商品库存和价格
		for(SundayProduct allProduct:allProducts){
			SundayPermissionDto permissionDto =
					new SundayPermissionDto(
							allProduct.getId(), allProduct.getName(),
							allProduct.getCategoryId(), allProduct.getStandard(),1,
							0, allProduct.getPrice().doubleValue(),
							0l);
			for(SundayPermission product_per:product_pers){
				if(LongUtil.isNotZreo(product_per.getOtherId())
						&&product_per.getType()==1
						&&allProduct.getId().longValue()==product_per.getOtherId().longValue()
						){
					//单独判断库存和价格
					if(product_per.getPrice()!=null){
						permissionDto.setPrice(product_per.getPrice());
					}
					if(product_per.getStore()!=null){
						permissionDto.setStore(product_per.getStore());
					}


					permissionDto.setPermissionId(product_per.getId());
					break;
				}
			}
			tree_products.add(permissionDto);

		}
		//4.1,组装二级分类和商品树形结构
		for(SundayPermissionDto tree_cat_child:tree_cat_children){
			List<SundayPermissionDto> children=new ArrayList<SundayPermissionDto>();
			for(SundayPermissionDto tree_product:tree_products){
				if(LongUtil.isNotZreo(tree_cat_child.getId())&&
						LongUtil.isNotZreo(tree_product.getParentId())&&
						tree_cat_child.getId().longValue()==tree_product.getParentId().longValue()){
					children.add(tree_product);
				}
			}
			tree_cat_child.setChildren(children);
		}
		//4.2,组装2级分类和和1级分类
		for(SundayPermissionDto tree_cat_parent:tree_cat_parents){
			List<SundayPermissionDto> children=new ArrayList<SundayPermissionDto>();
			for(SundayPermissionDto tree_cat_child:tree_cat_children){
				if(LongUtil.isNotZreo(tree_cat_parent.getId())&&
						LongUtil.isNotZreo(tree_cat_child.getParentId())&&
						tree_cat_parent.getId().longValue()==tree_cat_child.getParentId().longValue()){
					children.add(tree_cat_child);
				}
			}
			tree_cat_parent.setChildren(children);
		}
		return tree_cat_parents;
	}








	*//**
	 * 保存权限
	 * @param memberId
	 * @param otherIds
	 * @param otherId
	 * @param type 1,商品，2分类，3其他
	 * @param isOverwrite
	 *//*
	*//*public void savePermission(Long memberId,List<Long> otherIds,Long otherId,Integer type,boolean isOverwrite){
			if(ListUtil.isNull(otherIds))otherIds=new ArrayList<Long>();
			if(LongUtil.isNotZreo(otherId))otherIds.add(otherId);
			if(isOverwrite){
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("isDeleted",0);
				param.put("type",type);
				param.put("memberId",memberId);
				super.delete(param);
			}
			if(ListUtil.isNotNull(otherIds)){
				List<SundayPermission> permissions = new ArrayList<SundayPermission>();
				for(Long o:otherIds){
					permissions.add(new SundayPermission(memberId,o,type));
				}

				if(ListUtil.isNotNull(permissions))super.batchSave(permissions);
			}
	}*//*

	*//**获取权限
	 *
	 * @param memberId
	 * @param type
	 * @return
	 *//*
	public List<Long> getPermission(Long memberId, Integer type){
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("isDeleted",0);
			param.put("type",type);
			param.put("memberId",memberId);
			List<Long> otherIds=new ArrayList<Long>();

			for(SundayPermission permission:super.list(param)){
				otherIds.add(permission.getOtherId());
			}
			return otherIds;
	}


	*//**
	 * 修改商品的价格和库存
	 * @param memberId
	 * @param id
	 * @param price
	 * @param store
	 * @param productId
	 *//*
	public void change(Long memberId,Long id,Double price,Integer store,Long productId){
		SundayPermission permission=new SundayPermission();
		permission.setMemberId(memberId);
		permission.setId(id);
		permission.setType(1);
		permission.setOtherId(productId);
		permission.setStore(null);
		permission.setPrice(null);
		if(price!=null){
			permission.setPrice(price);
		}
		if(store!=null){
			permission.setStore(store);
		}

		if(LongUtil.isNotZreo(id)){
			super.updateExceptEmpty(id,permission);
		}else{
			//新增的时候需要校验是否存在
			//后期一般不会再新增
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("isDeleted",0);
			param.put("type",1);
			param.put("memberId",memberId);
			param.put("otherId",productId);
			List<SundayPermission> olds = super.list(param);
			if(ListUtil.isNotNull(olds)){
				super.updateExceptEmpty(olds.get(0).getId(),permission);
			}else{
				super.save(permission);
			}
			//super.save(permission);
		}

	}


}
*/
